/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seso;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.Image;

/**
 *
 * @author Administrator
 */
public class RBgButton extends javax.swing.JButton {

    private Image imageNormal = Util.getUserImage("resources/TextButtonNormal.png");
    private Image imageRover = Util.getUserImage("resources/TextButtonRover.png");
    
    private boolean focus_Gained = false, mouse_Entered = false;
    private final int arcWidthHeight = 8;

    public RBgButton() {
        super();
        initComponent();
        addListener();
    }

    public void initComponent() {
        
        setBorder(null);
        setBorderPainted(false);
        setContentAreaFilled(false);
        setFocusPainted(false);
        setOpaque(false);
        setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        setForeground(Color.WHITE);
        setFont(new Font("Arial Unicode MS", Font.PLAIN, 12));    
        setSize(100, 30);
        setPreferredSize(getSize());
    }

    private void addListener() {

        addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                setSelected(true);
                focus_Gained = true;
                repaint();
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                setSelected(false);
                focus_Gained = false;
                repaint();
            }
        });

        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouse_Entered = true;
                repaint();
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouse_Entered = false;
                repaint();
            }
        });

    }


    @Override
    protected void paintComponent(Graphics g) {
        if ((getIcon() != null)) {
            super.paintComponent(g);
            return;
        }

        Graphics2D g2d = (Graphics2D) g;
        int height = getHeight();
        int width = getWidth();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        RoundRectangle2D.Double rect;
        if (focus_Gained) {
            rect = new RoundRectangle2D.Double(0, 0, width, height, arcWidthHeight, arcWidthHeight);
        }  else {
            rect = new RoundRectangle2D.Double(3, 3, width - 6, height - 6, arcWidthHeight, arcWidthHeight);
        }
        g2d.clip(rect);

        g2d.drawImage((mouse_Entered) ? imageRover : imageNormal, 4, 4, width - 8, height - 8, null);
        
        super.paintComponent(g);
    }

    @Override
    protected void paintBorder(java.awt.Graphics g) {
        if ((getIcon() != null)) {
            super.paintBorder(g);
            return;
        }

        RoundRectangle2D.Double rect;
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setColor((focus_Gained) ? new Color(180, 209, 126) : this.getParent().getBackground());
        rect = new RoundRectangle2D.Double(3, 3, this.getWidth() - 7, this.getHeight() - 7, arcWidthHeight, arcWidthHeight);
        g2d.draw(rect);        

        g2d.setColor((focus_Gained) ? new Color(124, 157, 57) : this.getParent().getBackground());
        rect = new RoundRectangle2D.Double(2, 2, this.getWidth() - 5, this.getHeight() - 5, arcWidthHeight, arcWidthHeight);
        g2d.draw(rect);

        g2d.setColor((focus_Gained) ? new Color(183, 213, 115) : this.getParent().getBackground());
        rect = new RoundRectangle2D.Double(1, 1, this.getWidth() - 3, this.getHeight() - 3, arcWidthHeight, arcWidthHeight);
        g2d.draw(rect);

        g2d.setColor((focus_Gained) ? new Color(183, 213, 115) : this.getParent().getBackground());
        rect = new RoundRectangle2D.Double(0, 0, this.getWidth() - 1, this.getHeight() - 1, arcWidthHeight, arcWidthHeight);
        g2d.draw(rect);
    }
}
